using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using SQLite.CodeFirst;
using System.Data;

namespace WeManageService
{
    //自定义异常类
    public class DBException : ApplicationException
    {
        public string error;
        private Exception innerException;

        public DBException() { }

        public DBException(string msg) : base(msg)
        {
            this.error = msg;
        }

        public DBException(string msg,Exception innerexception) : base(msg, innerexception)
        {
            this.innerException = innerexception;
            this.error = msg;
        }

        public string GetError()
        {
            return error;
        }
    }

    //数据库任务类接口
    public class PlanDBManager
    {
        //--------------数据库接口-----------------
        //添加任务                  -addPlan
        //根据任务名删除任务        -deletePlan
        //根据全部信息修改任务      -modifyPlan
        //根据pState查询历史任务    -GetHistoryPlan
        //根据pState查询待完成任务  -GetUndonePlan
        //根据任务名查询任务        -GetPlan
        //查询全部任务              -GetAllPlan
        //查询某一用户的任务        -GetUserPlan

        public PlanDBManager() { }

        //添加任务*
        public void addPlan(Plan plan)
        {
            try
            {
                using (var context = new WMContext())
                {
                    UserDBManager userdb = new UserDBManager();

                    //plan中没有username或者为合法用户名
                    if (plan.UserName == null || userdb.GetUser(plan.UserName) != null)
                    {
                        //判断任务ID是否冲突
                        var existplan = context.Plans.FirstOrDefault(p => p.PlanID == plan.PlanID);
                        if (existplan == null)
                        {
                            context.Plans.Add(plan);
                            context.SaveChanges();
                        }
                        else
                        {
                            throw new DBException("任务ID冲突！");
                        }
                    }
                    else
                    {
                        throw new DBException("不存在此用户");
                    }
                }
            }
            //空参数异常
            catch(ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！",ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //根据planname删除任务*
        public void deletePlan(int PlanID)
        {
            try
            {
                using (var context = new WMContext())
                {
                    var plan = context.Plans.SingleOrDefault(p => p.PlanID == PlanID);
                    if (plan != null)
                    {
                        context.Plans.Remove(plan);
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new DBException("不存在该任务！");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //根据全部信息修改任务*
        public void modifyPlan(Plan newplan)
        {
            try
            {
                using (var context = new WMContext())
                {
                    var plan = context.Plans.SingleOrDefault(p => p.PlanID == newplan.PlanID);
                    if (plan != null)
                    {
                        plan.Name = newplan.Name;
                        plan.Description = newplan.Description;
                        plan.Address = newplan.Address;
                        plan.StartTime = newplan.StartTime;
                        plan.NotifyTime = newplan.NotifyTime;
                        plan.EndTime = newplan.EndTime;
						plan.LoopPeriod = newplan.LoopPeriod;
                        plan.Delayable = newplan.Delayable;
                        plan.Deletable = newplan.Deletable;
                        plan.PState = newplan.PState;
                        plan.Score = newplan.Score;
                        plan.Comment = newplan.Comment;
                        plan.UserName = newplan.UserName;
                        context.Entry(plan).State = EntityState.Modified;
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new DBException("不存在该任务！");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
        }

        //根据pState查询历史任务*
        public List<Plan> GetHistoryPlan()
        {
            try
            {
                using (var context = new WMContext())
                {
                    var planquery = context.Plans
                        .Where(p => p.PState == 1)
                        .OrderBy(p => p.EndTime);
                    return new List<Plan>(planquery);
                }
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //根据pState查询待完成任务*
        public List<Plan> GetUndonePlan()
        {
            try
            {
                using (var context = new WMContext())
                {
                    var planquery = context.Plans
                        .Where(p => p.PState == 0)
                        .OrderBy(p => p.StartTime);
                    return new List<Plan>(planquery);
                }
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //根据任务名查询任务*
        public Plan GetPlan(int planID)
        {
            try
            {
                using (var context = new WMContext())
                {
                    var plan = context.Plans.FirstOrDefault(p => p.PlanID == planID);
                    if (plan != null)
                    {
                        return plan;
                    }
                    else
                    {
                        return null;
                        throw new DBException("不存在该任务！");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //查询全部任务*
        public List<Plan> GetAllPlan()
        {
            try
            {
                using (var context = new WMContext())
                {
                    var planquery = context.Plans
                        .Where(p => p.PState == 0 || p.PState == 1)
                        .OrderBy(p => p.StartTime);
                    return new List<Plan>(planquery);
                }
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //查询某一用户的任务
        public List<Plan> GetUserPlan(string username)
        {
            try
            {
                using (var context = new WMContext())
                {
                    UserDBManager userdb = new UserDBManager();

                    //plan中没有username或者为合法用户名
                    if (username == null || userdb.GetUser(username) != null)
                    {
                        var planquery = context.Plans
                            .Where(p => p.UserName == username)
                            .OrderBy(p => p.StartTime);

                        return new List<Plan>(planquery);
                    }
                    else
                    {
                        throw new DBException("不存在此用户");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    //数据库愿望类接口
    public class WishDBManager
    {
        //--------------数据库接口-----------------
        //添加愿望                  -addWish
        //根据愿望名删除愿望        -deleteWish
        //根据全部信息修改愿望      -modifyWish
        //根据愿望名查询愿望        -GetWish
        //查询全部愿望              -GetAllWish
        //查询某一用户的愿望        -GetUserWish

        public WishDBManager() { }

        //添加愿望*
        public void addWish(Wish wish)
        {
            try
            {
                using (var context = new WMContext())
                {
                    UserDBManager userdb = new UserDBManager();

                    //wish中没有username或者为合法用户名
                    if (wish.UserName == null || userdb.GetUser(wish.UserName) != null)
                    {
                        //判断是否有重复的愿望名
                        var existwish = context.Wishes.FirstOrDefault(p => p.WishID == wish.WishID);
                        if (existwish == null)
                        {
                            context.Wishes.Add(wish);
                            context.SaveChanges();
                        }
                        else
                        {
                            throw new DBException("愿望名已存在，请重命名该愿望");
                        }
                    }
                    else
                    {
                        throw new DBException("不存在此用户");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //根据wishname删除愿望*
        public void deleteWish(int ID)
        {
            try
            {
                using (var context = new WMContext())
                {
                    var wish = context.Wishes.FirstOrDefault(p => p.WishID == ID);
                    if (wish != null)
                    {
                        context.Wishes.Remove(wish);
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new DBException("不存在该愿望！");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //根据全部信息修改愿望*
        public void modifyWish(Wish newwish)
        {
            try
            {
                using (var context = new WMContext())
                {
                    var wish = context.Wishes.FirstOrDefault(p => p.WishID == newwish.WishID);
                    if (wish != null)
                    {
                        wish.Name = newwish.Name;
                        wish.RequiredScore = newwish.RequiredScore;
                        wish.Details = newwish.Details;
                        context.Entry(wish).State = EntityState.Modified;
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new DBException("不存在该愿望！");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //根据愿望名查询愿望*
        public Wish GetWish(int wishID)
        {
            try
            {
                using (var context = new WMContext())
                {
                    var wish = context.Wishes.FirstOrDefault(p => p.WishID == wishID);
                    if (wish != null)
                    {
                        return wish;
                    }
                    else
                    {
                        return null;
                        throw new DBException("不存在该愿望！");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //查询全部愿望*
        public List<Wish> GetAllWish()
        {
            try
            {
                using (var context = new WMContext())
                {
                    var wishquery = context.Wishes
                        .Where(p => p.WishID != null);
                    return new List<Wish>(wishquery);
                }
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //查询某一用户的愿望*
        public List<Wish> GetUserWish(string username)
        {
            try
            {
                using (var context = new WMContext())
                {
                    UserDBManager userdb = new UserDBManager();

                    //wish中没有username或者为合法用户名
                    if (username == null || userdb.GetUser(username) != null)
                    {
                        var wishquery = context.Wishes
                            .Where(p => p.UserName == username);
                        return new List<Wish>(wishquery);
                    }
                    else
                    {
                        throw new DBException("不存在此用户");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    //数据库用户类接口
    public class UserDBManager
    {
        //--------------数据库接口-----------------
        //添加用户                  -addUser
        //根据全部信息修改用户      -modifyUser
        //根据用户名修改用户密码    -idModifyUserPassword
        //根据用户名查询用户        -GetUser
        //获取所有用户              -GetAllUser

        public UserDBManager() { }

        //添加用户*
        public void addUser(User user)
        {
            try
            {
                using (var context = new WMContext())
                {
                    //判断是否有重复的用户名
                    var existuser = context.Users.FirstOrDefault(p => p.UserName == user.UserName);
                    if (existuser == null)
                    {
                        context.Users.Add(user);
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new DBException("用户名已被使用，请重新命名！");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //获取所有用户
        public List<User> GetAllUsers()
        {
            var context = new WMContext();
            List<User> users = context.Users.ToList();
            return users;
        }

        //根据全部信息修改user *其中用户名必须是已经存在的
        public void modifyUser(User newuser)
        {
            try
            {
                using (var context = new WMContext())
                {
                    var user = context.Users.FirstOrDefault(u => u.UserName == newuser.UserName);
                    if (user != null)
                    {
                        user.AutoLogin = newuser.AutoLogin;
                        user.PassWord = newuser.PassWord;
                        user.Remember = newuser.Remember;
						user.Score = newuser.Score;
                        context.Entry(user).State = EntityState.Modified;
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new DBException("不存在此用户");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //根据userID修改用户密码 *其中用户名必须是已经存在的
        public void idModifyUserPassword(string username, string newpassword)
        {
            try
            {
                using (var context = new WMContext())
                {
                    var user = context.Users.FirstOrDefault(u => u.UserName == username);
                    if (user != null)
                    {
                        user.PassWord = newpassword;
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new DBException("不存在此用户");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //根据用户名查询用户 *其中用户名必须是已经存在的
        public User GetUser(string username)
        {
            try
            {
                using (var context = new WMContext())
                {
                    var user = context.Users.FirstOrDefault(u => u.UserName == username);
                    if (user != null)
                    {
                        return user;
                    }
                    else
                    {
                        return null;
                        throw new DBException("不存在此用户！");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    //数据库积分流水接口
    public class TurnoverDBManager
    {
        //--------------数据库接口-----------------
        //添加流水                  -AddTurnover
        //删除积分流水              -DeleteTurnover
        //获取所有流水              -GetAllTrunover
        //获取用户流水              -GetUserTrunover

        public TurnoverDBManager() { }

        //添加积分流水
        public void AddTurnover(Turnover turnover)
        {
            try
            {
                using (var context = new WMContext())
                {
                    UserDBManager userdb = new UserDBManager();

                    //turnover中没有username或者为合法用户名
                    if (turnover.UserName == null || userdb.GetUser(turnover.UserName) != null)
                    {
                        //判断是否有重复的任务名
                        context.Turnovers.Add(turnover);
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new DBException("不存在此用户！");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //删除积分流水
        public void DeleteTurnover(int turnoverid)
        {
            try
            {
                using (var context = new WMContext())
                {
                    var turnover = context.Turnovers.SingleOrDefault(p => p.TurnoverID == turnoverid);
                    if (turnover != null)
                    {
                        context.Turnovers.Remove(turnover);
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new DBException("不存在该积分流水记录！");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //查询全部积分流水
        public List<Turnover> GetAllTurnover()
        {
            try
            {
                using (var context = new WMContext())
                {
                    var turnoverquery = context.Turnovers
                        .Where(p => p.TurnoverID >=0 )
                        .OrderBy(p => p.Time);
                    return new List<Turnover>(turnoverquery);
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //查询某一用户的积分流水
        public List<Turnover> GetUserTurnover(string username)
        {
            try
            {
                using (var context = new WMContext())
                {
                    UserDBManager userdb = new UserDBManager();

                    //wish中没有username或者为合法用户名
                    if (username == null || userdb.GetUser(username) != null)
                    {
                        var turnoverquery = context.Turnovers
                            .Where(p => p.UserName == username)
                            .OrderBy(p => p.Time);
                        return new List<Turnover>(turnoverquery);
                    }
                    else
                    {
                        throw new DBException("不存在此用户");
                    }
                }
            }
            //空参数异常
            catch (ArgumentNullException ane)
            {
                throw new DBException("参数为空，请填入合法参数！", ane);
            }
            //参数异常
            catch (ArgumentException ae)
            {
                throw new DBException("参数非法异常！", ae);
            }
            //异常
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

}
